/*
wp.sqf
*/

#include "ACV_Constantes.h"
#include "ACV_ID_main_dlg.h"
#include "ACV_ID_wp_dlg.h"

private ["_grp", "_arr_wp","_forindex"];

disableSerialization;

_display = findDisplay ACV_DLG_ID_Main_Dlg;
_txt_sys = _display displayCtrl ACV_DLG_ID_Main_TxtSystem;
_map =  _display displayCtrl ACV_DLG_ID_Main_Map;
_list_units = _display displayCtrl ACV_DLG_ID_Main_LstStock;


_list_wp =  _display displayCtrl ACV_DLG_ID_Main_ListWp;
_list_wp_type =  _display displayCtrl ACV_DLG_ID_Main_CbWayPoint;
_add_wp = _display displayCtrl ACV_DLG_ID_Main_AddWp;
_edit_wp = _display displayCtrl ACV_DLG_ID_Main_EditWp;
_del_wp = _display displayCtrl ACV_DLG_ID_Main_DelWp;


_index_unit = lbcursel _list_units;

_index_wp = lbcursel _list_wp;
_number_wp = _list_wp lbValue _index_wp;


_grp = grpNull;

if(_index_unit > -1) then {
	_varname = _list_units lbData _index_unit;	
	call compile format ["_grp = %1", _varname];
};

_action = _this select 0;

if(_action == "list") then {
	if(_grp != grpNull) then {
		if(_index_wp > -1) then {
			_arr_wp = waypoints _grp;
			{
				_wp = _x;
				
				if( (_wp select 1) == _number_wp) then {
					
					_strtype = waypointType _wp;
					_strtype = [_strtype] call ACV_FNCT_GetFrenchMarker;
					_strtype = format["#%1 %2",_number_wp, _strtype];
					[(getWPPos _wp), _strtype] call ACV_FNCT_Show_WpMarker;
				};		
			} foreach _arr_wp;
			
			_edit_wp ctrlEnable true;
			_del_wp ctrlEnable true;
		};
	};
};

if(_action=="del") then {
	if(_grp != grpNull) then {
		_arr_wp = waypoints _grp;
		
		{
			_wp = _x;
			if( (_wp select 1) == _number_wp) then {
				deleteWaypoint _wp;
				lbclear _list_wp;
				_arr_wp = waypoints _grp;
				{
					_wp = _x;
					_no = (_wp select 1);
					_strtype = waypointType _wp;
					_strtype = [_strtype] call ACV_FNCT_GetFrenchMarker;
						
					_idx = _list_wp lbAdd format["#%1 %2",_no,_strtype];
					_list_wp lbSetValue [_idx, _no];
								
					_wp_pos = getWPPos _wp;
				} foreach _arr_wp;
				_edit_wp ctrlEnable false;
				_del_wp ctrlEnable false;
			};
		} foreach _arr_wp;
		
		
		call ACV_FNCT_Clear_WpMarker;
		_list_wp lbSetCurSel (count _arr_wp) - 1;
	};
};
if(_action=="add") then {
	if(_grp != grpNull) then {
		[_txt_sys, localize "STR_MSG_PLACE_WP"] spawn ACV_FNCT_FlashMessage;
		
		ACV_Pos = false;
		ACV_Void = false;
		ACV_Go = false;
		["R3F_ACV_AddWP_Click", "onMapSingleClick", { 
		ACV_Pos = _pos;ACV_Void = true;ACV_Go = true;
		}] call BIS_fnc_addStackedEventHandler;
		waitUntil {ACV_Void};
		["R3F_ACV_AddWP_Click", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	
		if( ! ACV_Go ) exitWith {};
	
		_pos = ACV_Pos;
	
		ACV_XXX = 0;
		ACV_DLG_RET = 1;
		_ok = createDialog "R3FACVWPDlg";
		waitUntil { ACV_XXX != 0 };
		ACV_Grp = _grp;	
		
		if(ACV_XXX==ACV_DLG_RET) then {
			sleep 0.5;
			lbclear _list_wp;
			_arr_wp = waypoints _grp;
			_forindex = 0;
			{
				_wp = _x;
				_no = (_wp select 1);
				_strtype = waypointType _wp;
				_strtype = [_strtype] call ACV_FNCT_GetFrenchMarker;
						
				_idx = _list_wp lbAdd format["#%1 %2",_no,_strtype];
				_list_wp lbSetValue [_idx, _no];
								
				_wp_pos = getWPPos _wp;
					
				//[_wp_pos,_strtype] call ACV_FNCT_Add_WpMarker;
				if( _index_wp == _forindex) then {
					call ACV_FNCT_Clear_WpMarker;
					[(getWPPos _wp), _strtype] call ACV_FNCT_Show_WpMarker;
				};
				_forindex = _forindex + 1;
			} foreach _arr_wp;
			
			_list_wp lbSetCurSel _index_wp;
		};
	};
};

if(_action=="edit") then {
	[_txt_sys, localize "STR_MSG_MOVE_WP"] spawn ACV_FNCT_FlashMessage;
	ACV_Pos = false;
	ACV_Void = false;
	ACV_Go = false;
	["R3F_ACV_EditWP_Click", "onMapSingleClick", { 
	ACV_Pos = _pos;ACV_Void = true;ACV_Go = true;
	}] call BIS_fnc_addStackedEventHandler;
	waitUntil {ACV_Void};
	["R3F_ACV_EditWP_Click", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;

	if( ! ACV_Go ) exitWith {};
	_pos = ACV_Pos;

	_arr_wp = waypoints _grp;
	
	_wp = _arr_wp select _index_wp;
	
	ACV_Wp = _wp;
	
	ACV_Default_WP_Type = waypointType _wp;
	ACV_Default_WP_Combat = waypointCombatMode _wp;
	ACV_Default_WP_Formation = waypointFormation _wp;
	ACV_Default_WP_Speed = waypointSpeed _wp;
	ACV_Default_WP_Behaviour = waypointBehaviour _wp;

	ACV_Default_WP_Placement = "0";
	ACV_Default_WP_Achevement = str(waypointCompletionRadius _wp);
	ACV_Default_WP_Delai_Min = str((waypointTimeout _wp) select 0);
	ACV_Default_WP_Delai_Moy = str((waypointTimeout _wp) select 1);
	ACV_Default_WP_Delai_Max = str((waypointTimeout _wp) select 2);
	ACV_Default_WP_Script = waypointStatements _wp;
	ACV_Default_WP_Condition = (waypointStatements _wp select 0);
	//diag_log format["ACV_Default_WP_Condition %1",ACV_Default_WP_Condition];
	ACV_XXX = 0;
	ACV_DLG_RET = 2;
	_ok = createDialog "R3FACVWPDlg";
	waitUntil { ACV_XXX != 0 };
	ACV_Grp = _grp;	
	if(ACV_XXX==ACV_DLG_RET) then {
		sleep 0.5;
		lbclear _list_wp;
		_arr_wp = waypoints _grp;
		_forindex = 0;
		{
			_wp = _x;
			_no = (_wp select 1);
			_strtype = waypointType _wp;
			_strtype = [_strtype] call ACV_FNCT_GetFrenchMarker;
					
			_idx = _list_wp lbAdd format["#%1 %2",_no,_strtype];
			_list_wp lbSetValue [_idx, _no];
							
			_wp_pos = getWPPos _wp;

			if( _index_wp == _forindex) then {
				call ACV_FNCT_Clear_WpMarker;
				[(getWPPos _wp), _strtype] call ACV_FNCT_Show_WpMarker;
			};
			_forindex = _forindex + 1;
		} foreach _arr_wp;
		_list_wp lbSetCurSel _index_wp;
	};

};
_txt_sys ctrlSetText " ";
sleep 0.03;
call ACV_FNCT_CancelMakeUnit;
_del_wp ctrlEnable ((count _arr_wp) > 1);